<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="author" content="order by dede58.com"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags
    -->
    <title>Float-Admin</title>

    <!-- Common plugins -->
    <link href="${pageContext.request.contextPath}../../static/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}../../static/css/simple-line-icons.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}../../static/css/font-awesome.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}../../static/css/pace.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}../../static/css/jasny-bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="${pageContext.request.contextPath}../../static/css/nanoscroller.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}../../static/css/metismenu.min.css">
    <!-- dataTables -->
    <link href="${pageContext.request.contextPath}../../static/css/jquery.datatables.min.css" rel="stylesheet"
          type="text/css">
    <link href="${pageContext.request.contextPath}../../static/css/responsive.bootstrap.min.css" rel="stylesheet"
          type="text/css">

    <!--template css-->
    <link href="${pageContext.request.contextPath}../../static/css/style.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="${pageContext.request.contextPath}../../static/js/html5shiv.min.js"></script>
    <script src="${pageContext.request.contextPath}../../static/js/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<!--page header start-->
<div class="page-header">
    <div class="row">
        <div class="col-sm-6">
            <h4>仓库盘点</h4>
        </div>
        <div class="col-sm-6 text-right">
            <ol class="breadcrumb">
                <li><a href="javascript: void(0);"><i class="fa fa-home"></i></a></li>
                <li>E-commerce</li>
                <li>仓库盘点</li>
            </ol>
        </div>
    </div>
</div>
<!--page header end-->


<!--start page content-->
<label for="pdType">单据类型：</label>
<br>
<select name="pdType" id="pdType" style="width: 200px;height: 30px;border: 1px solid #dde6e9;">
    <option value="-1">请选择</option>
    <option value="1">原材料仓库盘点</option>
</select>
<br>
<label for="pdVoucher">单据编号：</label>
<input type="text" name="pdVoucher" id="pdVoucher" value="自动生成无需填写" readonly="readonly" style="width: 200px">
<label for="pdDetail">盘点日期：</label>
<br>
    <input type="text" name="pdVoucher" id="pdDetail" value="自动生成无需填写" readonly="readonly" style="width: 200px">
<br>
<label for="pdMaterial">请选择要盘点的材料：</label>
<br>
<select name="pdMaterial" id="pdMaterial" onchange="getPDMaterialWarehouse()"
        style="width: 200px;height: 30px;border: 1px solid #dde6e9;">
    <option value="-1">请选择</option>
</select>
<br>
<label for="pdVoucher">盘点明细：</label>
<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <table id="datatable1" class="table table-striped dt-responsive nowrap table-hover">
                    <thead>
                    <tr>
                        <th></th>
                        <th>材料批次号</th>
                        <th>材料品种</th>
                        <th>规格型号</th>
                        <th>材料单位</th>
                        <th>材料单价</th>
                        <th>库存数量</th>
                        <th>盘点数量</th>
                        <th>盘盈盘亏</th>
                        <th>盈亏数量</th>
                    </tr>
                    <tr id="tr1">
                        <td>1</td>
                        <td>
                            <select name="batchNumber" id="batchNumber" onchange="getPDMaterialMSG()"
                                    style="width: 200px;border: 1px solid #dde6e9;">
                                <option value="-1">请选择</option>
                            </select>
                        </td>
                        <td>暂无内容</td>
                        <td>暂无内容</td>
                        <td>暂无内容</td>
                        <td>暂无内容</td>
                        <td id="beforeNums">暂无内容</td>
                        <td>
                            <input type="text" id="reality" onblur="surplusOrLoss()"
                                   style="width: 100px;height: 20px">
                        </td>
                        <td>暂无内容</td>
                        <td>暂无内容</td>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>
<label for="pdRemark">盘点备注：</label>
<input type="text" name="pdRemark" id="pdRemark" style="width: 200px">
<label for="principal">盘点人：</label>
<input type="text" name="principal" id="principal" style="width: 200px">
<button onclick="addPDJL()">提交</button>
<!--end page content-->
<!--Common plugins-->
<script src="${pageContext.request.contextPath}../../static/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}../../static/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}../../static/js/pace.min.js"></script>
<script src="${pageContext.request.contextPath}../../static/js/jasny-bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}../../static/js/jquery.slimscroll.min.js"></script>
<script src="${pageContext.request.contextPath}../../static/js/jquery.nanoscroller.min.js"></script>
<script src="${pageContext.request.contextPath}../../static/js/metismenu.min.js"></script>
<script src="${pageContext.request.contextPath}../../static/js/float-custom.js"></script>
<!-- Datatables-->
<script src="${pageContext.request.contextPath}../../static/js/jquery.datatables.min.js"></script>
<script src="${pageContext.request.contextPath}../../static/js/datatables.responsive.min.js"></script>
</body>
<script>
    getMaterials();

    function getMaterials() {
        $.ajax({
            "url": "huaPDWarehouse/getPDMaterial",
            "type": "get",
            "data": {},
            "dataType": "json",
            "success": function (result) {
                console.log(result)
                let cc = "";
                for (let index in result) {
                    cc += `
    <option value="${result[index].materialId}">${result[index].materialName}</option>
    `
                }
    $("#pdMaterial").append(cc)
            },
            "error": function () {
                alert("出错了")
            }
        })
    }

    function getPDMaterialWarehouse() {
        let materialId = $("#pdMaterial").val()
        $.ajax({
            "url": "huaPDWarehouse/getPDMaterialWarehouse",
            "type": "get",
            "data": {materialId: materialId},
            "dataType": "json",
            "success": function (result) {
                console.log(result)
                let cc = "";
                for (let index in result) {
                    cc += `
    <option value=${result[index].materialCode}>${result[index].materialCode}</option>
    `
                }
    $('#batchNumber option').not('option:first').remove();
                $("#batchNumber").append(cc)
            },
            "error": function () {
                alert("出错了")
            }
        })
    }

    function getPDMaterialMSG() {
        let materialCode = $("#batchNumber").val();
        console.log(materialCode)
        $.ajax({
            "url": "huaPDWarehouse/getMaterialDetailPd",
            "type": "get",
            "data": {materialCode: materialCode},
            "dataType": "json",
            "success": function (result) {
                console.log(result)
                let materialName = result[0].materialName
                let materialPlyName = result[0].materialPlyName
                let unitName = result[0].unitName
                let materialInWarehousePrice = result[0].materialInWarehousePrice
                let materialNums = result[0].materialNums
                $("#tr1").children("td").eq(2).text(materialName)
                $("#tr1").children("td").eq(3).text(materialPlyName)
                $("#tr1").children("td").eq(4).text(unitName)
                $("#tr1").children("td").eq(5).text(materialInWarehousePrice)
                $("#tr1").children("td").eq(6).text(materialNums)
            },
            "error": function () {
                alert("出错了")
            }
        })
    }

    function surplusOrLoss() {
        let before = $("#beforeNums").text()
        let real = $("#reality").val()
        let rel = before - real
        if (rel > 0) {
            $("#tr1").children("td").eq(8).text("盘亏")
            $("#tr1").children("td").eq(9).text(rel)
        } else if (rel = 0) {
            $("#tr1").children("td").eq(8).text("不盈不亏")
            $("#tr1").children("td").eq(9).text(rel)
        } else {
            $("#tr1").children("td").eq(8).text("盘盈")
            $("#tr1").children("td").eq(9).text(rel)
        }
    }


    function addPDJL() {
        // 获取当前时间
        let currentDate = new Date();
        // 获取年月日
        let year = currentDate.getFullYear();
        let month = currentDate.getMonth() + 1; // 注意月份从0开始，需要加1
        let day = currentDate.getDate();
        // 获取时分秒
        let hours = currentDate.getHours();
        let minutes = currentDate.getMinutes();
        let seconds = currentDate.getSeconds();

        // 补零函数
        function padZero(number) {
            return (number < 10 ? '0' : '') + number;
        }

        // 格式化时间
        let formattedDate = year + padZero(month) + padZero(day) + padZero(hours) + padZero(minutes) + padZero(seconds);
        // 输出结果
        console.log(formattedDate);
        console.log("111111111111");
        let pdType = $("#pdType option:selected").val();
        let pdTypeNum = $("#pdType option:selected").val();
        let vou;
        if (pdTypeNum == 1) {
            vou = "YCL"
        }
        let pdVoucher = vou + formattedDate
        let pdMaterial = $("#pdMaterial").val();
        let batchNumber = $("#batchNumber option:selected").text();
        let i;
        let before = $("#beforeNums").text();
        let real = $("#reality").val();
        let rel = before - real;
        if (rel > 0) {
            i = 1;
        } else if (rel === 0) {
            i = 2;
        } else {
            i = 0;
        }
        let surOrLess = i;
        let surOrLessNums = rel;
        let price = $("#tr1").children("td").eq(5).text();
        let surOrLessPrice = price * surOrLessNums
        let pdRemark = $("#pdRemark").val();
        let principal = $("#principal").val();
        let data = {
            receiptsType: pdType,
            receiptsId: pdVoucher,
            receiptsMaterialId: pdMaterial,
            receiptsBatchCode: batchNumber,
            receiptsSurplusOrLoss: surOrLess,
            receiptsSurplusOrLossNums: surOrLessNums,
            receiptsSurplusOrLossPrice: surOrLessPrice,
            receiptsRemark: pdRemark,
            receiptsPrincipal: principal
        }
        console.log(data)
        let nr = JSON.stringify(data)
        console.log(nr)
        $.ajax({
            "url": "huaPDWarehouse/addWarehousePD",
            "type": "post",
            "data": nr,
            "contentType": "application/json;charset=utf-8;",
            "dataType": "json",
            "success": function (result) {
                if (result.code === 200) {
                    alert("提交盘点记录成功")
                } else {
                    alert("提交盘点记录失败")
                }
            },
            "error": function () {
                alert("出错了")
            }
        })
    }
</script>
</html>